#include<iostream>
#include<vector>
using namespace std;

class Solution {
    int dirx[8] = { -1, -1, -1, 0, 0, 1, 1, 1 };
    int diry[8] = { -1, 0, 1, -1, 1, -1, 0, 1 };
public:
    void gameOfLife(vector<vector<int>>& board) {
        int m = board.size();
        int n = board[0].size();
        vector<vector<int>> remArr(m, vector<int>(n, 0));
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                int liveCellCount = 0;
                for (int k = 0; k < 8; k++) {
                    int ii = i + dirx[k];
                    int jj = j + diry[k];
                    if (ii >= 0 && ii < m && jj >= 0 && jj < n && board[ii][jj]) {
                        liveCellCount++;
                    }
                }
                if (liveCellCount < 2) remArr[i][j] = 0;
                else if (liveCellCount == 2 && board[i][j] == 1) remArr[i][j] = 1;
                else if (liveCellCount == 3) remArr[i][j] = 1;
                else if (liveCellCount > 3) remArr[i][j] = 0;
            }
        }
        swap(board, remArr);
    }
};


